﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WestWorldRemake.Characters;
using WestWorldRemake.Messaging;
using WestWorldRemake.Characters;

namespace WestWorldRemake.States
{
    public class MinerOwned
    {
        public class EnterMineAndDigForNugget : State<Miner>
        {
            private EnterMineAndDigForNugget() { }
            private static EnterMineAndDigForNugget _Instance;
        
            public static EnterMineAndDigForNugget Instance
            {
                get
                {
                    if (_Instance == null)
                        _Instance = new EnterMineAndDigForNugget();
                    return _Instance;
                }
            }

            public override void Enter(Miner miner)
            {
                if (miner.Location != LocationType.Goldmine)
                {
                    miner.ChangeLocation(LocationType.Goldmine);
                    Utilities.WriteText(miner, "Walkin' to the mine.");
                }
            }
            public override void Execute(Miner miner)
            {
                miner.AddToGoldCarried(1);
                miner.IncreaseFatigue();

                Utilities.WriteText( miner, "Pickin' up a nugget");

                if (miner.PocketsFull())
                    miner.StateMachine.ChangeState(VistBankAndDepositGold.Instance);
                if (miner.IsThirsty())
                    miner.StateMachine.ChangeState(QuenchThirst.Instance);
            }
            public override void Exit(Miner miner)
            {
                Utilities.WriteText( miner, "Ah'm leavin' the goldmine with mah pockets full o' gold");
            }

            public override Iffy OnMessage(Miner character, Telegram t)
            {
                return Iffy.No;
            }
        }

        public class VistBankAndDepositGold : State<Miner>
        {
            private VistBankAndDepositGold() { }
            private static VistBankAndDepositGold _Instance;

            public static VistBankAndDepositGold Instance
            {
                get
                {
                    if (_Instance == null)
                        _Instance = new VistBankAndDepositGold();
                    return _Instance;
                }
            }

            public override void Enter( Miner miner)
            {
                if (miner.Location != LocationType.Bank)
                {
                    Utilities.WriteText( miner, "Goin' to the bank. Yes siree");

                    miner.ChangeLocation(LocationType.Bank);
                }
            }
            public override void Execute( Miner miner)
            {
                miner.AddToWealth(miner.GetGoldCarried());
                miner.SetGoldCarred(0);

                Utilities.WriteText( miner, "Depositing gold. Total savings now: " + miner.Wealth());

                if (miner.Wealth() >= miner.ComfortLevel)
                {
                    Utilities.WriteText( miner, "WooHoo! Rich now.  Back home to mah li'lle lady.");

                    miner.StateMachine.ChangeState(GoHomeAndSleepTilRested.Instance);
                }
                else
                {
                    miner.StateMachine.ChangeState(EnterMineAndDigForNugget.Instance);
                }
            }
            public override void Exit( Miner miner)
            {
                Utilities.WriteText( miner, "Leavin' the bank");
            }

            public override Iffy OnMessage(Miner character, Telegram t)
            {
                return Iffy.No;
            }
        }

        public class GoHomeAndSleepTilRested : State<Miner>
        {
            private GoHomeAndSleepTilRested() { }
            private static GoHomeAndSleepTilRested _Instance;

            public static GoHomeAndSleepTilRested Instance
            {
                get
                {
                    if (_Instance == null)
                        _Instance = new GoHomeAndSleepTilRested();
                    return _Instance;
                }
            }

            public override void Enter( Miner miner)
            {
                if (miner.Location != LocationType.Shack)
                {
                    Utilities.WriteText( miner, "Walkin' home");

                   miner.ChangeLocation(LocationType.Shack);

                    MessageDispatcher.Instance.DispatchMessage(MessageDispatcher.SendMessageImmediately, miner, 
                        EntityManager.Instance.GetEntityByName("Elsa"), new Messages.HiHoneyImHome(), MessageDispatcher.NoAdditionalInfo);
                }
            }
            public override void Execute( Miner miner)
            {
                if (!miner.IsFatigued())
                {
                    Utilities.WriteText( miner, "What a nice nap! Time to find more gold!");

                    miner.StateMachine.ChangeState(EnterMineAndDigForNugget.Instance);
                }
                else
                {
                    miner.DecreaseFatigue();
                    Utilities.WriteText( miner, "ZZZZZ....");
                }
            }
            public override void Exit( Miner miner)
            {
                Utilities.WriteText( miner, "Leaving the house.");
            }

            public override Iffy OnMessage(Miner character, Telegram t)
            {
                if (t.Message.IsOfType(typeof(Messages.StewReady)))
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("\nMessage handled by " + character.name + " at time " + DateTime.Now.ToShortTimeString());

                    Utilities.WriteText(character, "Ok hun, ahm a comin'!");
                    character.StateMachine.ChangeState(MinerOwned.EatStew.Instance);

                    return Iffy.Yes;
                }
                return Iffy.No;
            }
        }

        public class QuenchThirst : State<Miner>
        {
            private QuenchThirst() { }
            private static QuenchThirst _Instance;

            public static QuenchThirst Instance
            {
                get
                {
                    if (_Instance == null)
                        _Instance = new QuenchThirst();
                    return _Instance;
                }
            }
            public override void Enter( Miner miner)
            {
                if (miner.Location != LocationType.Saloon)
                {
                    miner.ChangeLocation(LocationType.Saloon);

                    Utilities.WriteText( miner, "Boy, ah sure is thusty! Walkin' to the saloon.");
                }
            }
            public override void Execute( Miner miner)
            {
                if (miner.IsThirsty())
                {
                    miner.BuyAndDrinkAWhiskey();

                    Utilities.WriteText( miner, "That's mighty fine sippin' liquer.");

                    miner.StateMachine.ChangeState(EnterMineAndDigForNugget.Instance);
                }
                else
                    Utilities.WriteText( miner, "Error! Error! Error!");
            }
            public override void Exit( Miner miner)
            {
                Utilities.WriteText(miner, "Leaving the saloon, feelin' good");
            }

            public override Iffy OnMessage(Miner character, Telegram t)
            {
                return Iffy.No;
            }
        }
        
        public class EatStew : State<Miner>
        {
            private EatStew() { }
            private static EatStew _Instance;

            public static EatStew Instance
            {
                get
                {
                    if (_Instance == null)
                        _Instance = new EatStew();
                    return _Instance;
                }
            }

            public override void Enter(Miner entity)
            {
                Utilities.WriteText(entity, "Smells reaaal goood Elsa!");
            }
            public override void Execute(Miner entity)
            {
                Utilities.WriteText(entity, "Tastes real good too!");

                entity.StateMachine.RevertToPreviousState();
            }
            public override void Exit(Miner entity)
            {
                Utilities.WriteText(entity, "Thankay li'lle lady. Ah better get back to whatever ah wuz doing.");
            }

            public override Iffy OnMessage(Miner entity, Telegram t)
            {
                return Iffy.No;
            }
        }
        
    }
}
